"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _useFetch2 = _interopRequireDefault(require("../useFetch"));
/**
 * 用户信息处理 hooks
 * @param routes
 * @param options
 */
function useGlobalUser(request, options) {
  var IS_LOCAL = process.env.NODE_ENV === 'development';
  var _ref = options || {},
    transform = _ref.transform,
    defaultLocalUser = _ref.defaultLocalUser;
  var _useState = (0, _react.useState)(IS_LOCAL && defaultLocalUser ? defaultLocalUser : {}),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    userInfo = _useState2[0],
    setUserInfo = _useState2[1];
  var _useFetch = (0, _useFetch2.default)(function (params) {
      return request(params).then(function (res) {
        if (res && res.success) {
          var convertedUserInfo = transform ? transform(res.data) : res.data;
          setUserInfo(convertedUserInfo);
        }
        return res;
      });
    }, {
      manual: true
    }),
    fetchUserInfo = _useFetch.fetch;
  var isLogined = (0, _react.useMemo)(function () {
    return !!(userInfo && Object.keys(userInfo).length);
  }, [userInfo]);
  return {
    userInfo: userInfo,
    setUserInfo: setUserInfo,
    fetchUserInfo: fetchUserInfo,
    isLogined: isLogined
  };
}
var GlobalUserContext = /*#__PURE__*/(0, _react.createContext)({});
useGlobalUser.GlobalUserContext = GlobalUserContext;
function useGlobalUserContext() {
  return (0, _react.useContext)(GlobalUserContext);
}
useGlobalUser.useGlobalUserContext = useGlobalUserContext;
var _default = useGlobalUser;
exports.default = _default;